/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ExcluirConsulta.java
 *
 * Created on 21/11/2011, 23:09:39
 */

package view;

import control.ConsultaControler;
import java.util.ArrayList;
import javax.swing.JOptionPane;

/**
 *
 * @author tais
 */
public class ExcluirConsulta extends javax.swing.JFrame {
    int medicoSelecionado = 0;
    boolean carregou_medicos = false;
    private ArrayList<Object> medicos = ConsultaControler.getAllMedicos();

    /** Creates new form ExcluirConsulta */
    public ExcluirConsulta() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtf_cpfPaciente = new javax.swing.JTextField();
        dadosPaciente = new javax.swing.JLabel();
        dadosMedico = new javax.swing.JLabel();
        jcb_lista_medicos = new javax.swing.JComboBox();
        botaoRemover = new javax.swing.JButton();
        label_avisos = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("DESMARCAR CONSULTA");
        setName("EXCLUIR CONSULTA"); // NOI18N

        dadosPaciente.setText("CPF do Paciente");

        dadosMedico.setText("Medico");

        jcb_lista_medicos.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jcb_lista_medicosMousePressed(evt);
            }
        });
        jcb_lista_medicos.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                jcb_lista_medicosItemStateChanged(evt);
            }
        });

        botaoRemover.setText("Desmarcar");
        botaoRemover.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                botaoRemoverActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(114, 114, 114)
                .addComponent(botaoRemover, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(126, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addComponent(dadosPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 125, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jtf_cpfPaciente, javax.swing.GroupLayout.DEFAULT_SIZE, 181, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(dadosMedico, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGap(56, 56, 56)
                        .addComponent(jcb_lista_medicos, javax.swing.GroupLayout.PREFERRED_SIZE, 218, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(54, 54, 54))
            .addComponent(label_avisos, javax.swing.GroupLayout.DEFAULT_SIZE, 390, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(30, 30, 30)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dadosMedico, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jcb_lista_medicos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(dadosPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jtf_cpfPaciente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(label_avisos, javax.swing.GroupLayout.DEFAULT_SIZE, 21, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(botaoRemover)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jcb_lista_medicosItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_jcb_lista_medicosItemStateChanged
       medicoSelecionado = jcb_lista_medicos.getSelectedIndex();
}//GEN-LAST:event_jcb_lista_medicosItemStateChanged

    private void jcb_lista_medicosMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jcb_lista_medicosMousePressed
       if(carregou_medicos == false){
            jcb_lista_medicos.removeAllItems();

            jcb_lista_medicos.addItem("Selecione o médico");
            for (int i = 0; i < medicos.size(); i++){
              jcb_lista_medicos.addItem(medicos.get(i).toString());
            }

            carregou_medicos = true;
        }
}//GEN-LAST:event_jcb_lista_medicosMousePressed

    private void botaoRemoverActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_botaoRemoverActionPerformed
        Object medico;
        //String cpf;
        if(ConsultaControler.pacienteEhValido(jtf_cpfPaciente.getText())){
            if(medicoSelecionado == 0){
                medico = null;
            }
            else{
                medico = medicos.get(medicoSelecionado-1);
            }
            /*if(jtf_cpfPaciente.getText().isEmpty()){
                cpf = null;
            }
            else cpf = jtf_cpfPaciente.getText();*/

            ArrayList<Object> consultas =
                    ConsultaControler.buscar_lista_consultas(medico,jtf_cpfPaciente.getText());

            if(consultas.size() == 0){
                label_avisos.setText("Nenhuma consulta foi encontrada");
            }
            else if(consultas.size() == 1){
                int n = JOptionPane.showConfirmDialog(this,
                        ""+consultas.get(0).toString()+
                        "\nTem certeza que deseja exluir esta consulta?",
                        "Excluir Consulta",JOptionPane.YES_NO_OPTION);
                if(n == JOptionPane.YES_OPTION){
                    ConsultaControler.exclui_consulta(consultas.get(0));
                    label_avisos.setText("Consulta excluída!.");
                    this.dispose();
                }
                else label_avisos.setText("Consulta inalterada.");
            }
            else{
                Object[] options = new Object[consultas.size()];
                int i;

                for(i=0; i< consultas.size(); i++){
                    options[i] = i+"-"+consultas.get(i).toString();
                }

                String input = (String) JOptionPane.showInputDialog(null, "Mais de uma consulta encontrada.",
                "Escolha a consulta a ser excluida", JOptionPane.QUESTION_MESSAGE, null,
                 options, options[0]);
                
               if(null != input && input.isEmpty() == false){
                    i=0; int in=0;
                    while(input.substring(i, i+1).equals("-")==false){
                        in = in*10 + Integer.parseInt(input.substring(i, i+1));
                        i++;
                    }

                     int n = JOptionPane.showConfirmDialog(this,
                            ""+consultas.get(in).toString()+
                            "\nTem certeza que deseja exluir esta consulta?",
                            "Excluir Consulta",JOptionPane.YES_NO_OPTION);
                    if(n == JOptionPane.YES_OPTION){
                        ConsultaControler.exclui_consulta(consultas.get(in));
                        label_avisos.setText("Consulta excluída!.");
                        this.dispose();
                    }
                    else label_avisos.setText("Consulta inalterada.");
                    }
               }
               
        }
        else{
            if(jtf_cpfPaciente.getText().isEmpty()){
                label_avisos.setText("Por favor, informe o CPF do paciente.");
            }
            else label_avisos.setText("Paciente não encontrado.");
        }
    }//GEN-LAST:event_botaoRemoverActionPerformed

    /**
    * @param args the command line arguments
    */
  /*  public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new ExcluirConsulta().setVisible(true);
            }
        });
    }*/

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton botaoRemover;
    private javax.swing.JLabel dadosMedico;
    private javax.swing.JLabel dadosPaciente;
    private javax.swing.JComboBox jcb_lista_medicos;
    private javax.swing.JTextField jtf_cpfPaciente;
    private javax.swing.JLabel label_avisos;
    // End of variables declaration//GEN-END:variables

}
